/** 
 场外期权生命周期管理
*/
import React, { PureComponent } from 'react';
import { DATA_ENTRY, AdvancedSearch, ContentPackage, codeType } from '../../../comps';
import { connect } from 'dva';
import moment from 'moment';
import OTCOptionLifeCycleTable from './OTCOptionLifeCycleTable';
import ShareOption from '../shareOption/shareOption';
class OTCOptionLifeCycleManagement extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      optionEnentType: [], //事件类型
      optionStruct: [], //期权结构
      bundleId: '',
      isShow: false, // 是否展示详细信息
      searchObj: {
        date: moment().format('YYYYMMDD'),
      },
      num: 1,
    };
  }

  componentDidMount() {
    const { searchObj } = this.state;
    const {
      globalM: { dataDictionary = {} },
    } = this.props;
    // 期权结构枚举接口
    this.props.dispatch({
      type: 'OTCOptionLifeCycleManagementM/getOptionStructData',
      payload: {
        dictData: dataDictionary,
      },
      callback: (type, res) => {
        if (type == 'success') {
          let tempArr1 = [];
          let tempArr2 = [];
          res.OTC_OPTION_BOOK_OPTIONSTRUCT.forEach((item) => {
            tempArr1.push({ title: item.value, value: item.code });
          });
          res.OTC_OPTION_BOOK_ENENT_TYPE.forEach((item) => {
            tempArr2.push({ title: item.value, value: item.code });
          });
          this.setState({
            optionStruct: tempArr1, //期权结构
            optionEnentType: tempArr2.filter((x) => x.code != 0), //事件类型 去除'无'的选项
          });
        }
      },
    });
  }

  UNSAFE_componentWillReceiveProps(nextProps) {}

  //交易详情
  showShareOption = (e) => {
    this.setState({
      bundleId: e,
    });
  };

  //返回
  onBack = (e) => {
    this.setState({
      bundleId: '',
    });
  };

  render() {
    const { searchObj, bundleId, optionEnentType, num } = this.state;
    const searchProps = {
      dataSource: [
        {
          dataIndex: 'date',
          title: '可执行日期 ',
          type: DATA_ENTRY.SINGLEDATE,
          initialValue: moment(),
          allowClear: false,
        },
        {
          dataIndex: 'type',
          title: '事件类型',
          type: DATA_ENTRY.SELECT,
          dataSource: optionEnentType,
          allowClear: true,
        },
        {
          dataIndex: 'portId',
          title: '产品名称',
          type: DATA_ENTRY.PRODUCTLIST,
          multiple: true,
          dataType: 2,
          allowClear: true,
          tradeModel: ['BOOKKEEPING_MODE', 'COMMAND_MODE'], //只有簿记和指令模式产品
        },
        {
          dataIndex: 'optionStruct',
          title: '期权结构',
          type: DATA_ENTRY.SELECT,
          dataSource: this.state.optionStruct,
          // loading: this.state.loading,
          isMultiple: true,
          allowClear: true,
        },
        {
          dataIndex: 'tradingCode',
          title: '标的证券',
          type: DATA_ENTRY.SECURITIESCODE,
          codeType: codeType.cwqq,
          maxTagCount: 0,
          allowClear: true,
        },
        {
          dataIndex: 'counterpartyid',
          title: '交易对手',
          type: DATA_ENTRY.TRADINGCOMPANY,
          dataType: 2,
          allowClear: true,
        },
      ],
      onQuery: (e) => {
        this.setState({
          searchObj: {
            ...e,
            date: e.date.format('YYYYMMDD'),
          },
          num: this.state.num + 1,
        });
      },
      onClear: () => {},
    };

    return (
      <ContentPackage
        title='期权生命周期管理'
        contentHtml={
          <div style={{ height: '100%' }}>
            <div style={{ display: !bundleId ? 'block' : 'none' }}>
              <div style={{ marginBottom: '20px' }}>
                <AdvancedSearch {...searchProps} />
              </div>
              <div>
                <OTCOptionLifeCycleTable searchObj={searchObj} num={num} showShareOption={this.showShareOption} optionEnentType={optionEnentType}></OTCOptionLifeCycleTable>
              </div>
            </div>
            {bundleId && (
              <div>
                <ShareOption onBack={this.onBack} showShareOption={this.showShareOption} type='4' bundleId={bundleId} />
              </div>
            )}
          </div>
        }
      />
    );
  }
}
export default connect(({ OTCOptionLifeCycleManagementM, loading, globalM }) => ({
  OTCOptionLifeCycleManagementM,
  loading,
  globalM,
}))(OTCOptionLifeCycleManagement);
